package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.CarManage;
import com.qf.administration.mapper.CarManageMapper;
import com.qf.administration.qo.CarManageQO;
import com.qf.administration.service.CarManageService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.CarManageVO;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;


@Service
public class CarManageServiceImpl implements CarManageService {
    @Resource
    private CarManageMapper carManageMapper;

    @Override
    public RespResult<String> addCarManage(CarManageQO carManageQO) {
        CarManage carManage = carManageMapper.selectById(carManageQO.getCarId());
        if (ObjectUtils.isEmpty(carManage)) {
            /*id不存在，则可以进行添加*/
            CarManage carManage1 = new CarManage();
            BeanUtils.copyProperties(carManageQO, carManage1);
            int insert = carManageMapper.insert(carManage1);
            if (insert > 0) {
                return RespResult.success("会议室添加成功");
            } else {
                throw new ServiceException(ResultCode.SYS_ERROR);
            }
        } else {
            /*id存在，添加失败*/

            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> deleteCarManage(int[] ids) {
        int count = carManageMapper.deleteByMemberId(ids);
        if (count != ids.length) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        } else {
            return RespResult.success("删除成功");
        }
    }

    @Override
    public RespResult<String> updateCarManage(CarManageQO carManageQO) {
        CarManage carManage = new CarManage();
        BeanUtils.copyProperties(carManageQO, carManage);
        int i = carManageMapper.updateById(carManage);
        if (i < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("更新成功");
    }

    @Override
    public PageInfo<CarManageVO> queryCarManage(int page, int size, String busNumber, String saddle, String carSortName, String carBrand, String carStatus) {
        PageInfo<CarManage> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> carManageMapper.selectList(busNumber, saddle, carSortName, carBrand, carStatus));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), CarManageVO::new);
    }

    @Override
    public CarManageVO queryCarManageOne(int id) {
        CarManage carManage = carManageMapper.selectById(id);
        if (ObjectUtils.isEmpty(carManage)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            CarManageVO carManageVO = new CarManageVO();
            BeanUtils.copyProperties(carManage, carManageVO);
            return carManageVO;
        }
    }

}
